package controller;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXRadioButton;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import sample.Register;
import sql.RegisterJDBC;

import java.util.regex.*;

public class RegisterController {
    @FXML
    private TextField username;
    @FXML
    private PasswordField password;
    @FXML
    private PasswordField password2; //确认密码
    @FXML
    private JFXButton registerbutton;
    @FXML
    private ChoiceBox<String> provinces;
    @FXML
    private JFXDatePicker birth;
    @FXML
    private  JFXRadioButton jfxRadioButton1;
    @FXML
    private  JFXRadioButton jfxRadioButton11;

    public void initialize(){ //创造对应窗口时得初始化函数
        provinces.setItems(FXCollections.observableArrayList("河北省","山西省","吉林省","辽宁省","黑龙江省","陕西省","甘肃省","青海省","山东省","福建省","浙江省","台湾省",
                "河南省","湖北省" ,"湖南省","江西省","江苏省","安徽省","广东省","海南省","四川省","贵州省","云南省"));
    }
//、、、、、、、。
    public void click() throws Exception{
        String reg1 = "^[A-Za-z]+\\w*$";
        String reg2="^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,15}$"; //密码匹配正则  强密码(必须包含大小写字母和数字的组合，可以使用特殊字符，长度在8-10之间)
        String pass=password.getText();
        String user=username.getText();
        String birthday ;
        String pass2 = password2.getText();
        String gender,identity;
        String address;
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Information Dialog");
        alert.setHeaderText("提示:");
        if(pass.isEmpty()||pass2.isEmpty()||user.isEmpty()){
            //警告空
            alert.setContentText("请确认输入，密码和用户名不能为空！！");
            alert.showAndWait();
        }
        else{
            if(!pass2.equals(pass)){
                alert.setContentText("请确认两次输入密码一致！！！！");
                alert.showAndWait();
            }
            boolean ismatch1 = Pattern.matches(reg1,user);
            if(ismatch1) { //匹配
                boolean ismatch2 = Pattern.matches(reg2, pass);
                if(!ismatch2){
                    alert.setContentText("请确认输入账户名为字母开头加数字，密码为强密码(必须包含大小写字母和数字的组合，可以使用特殊字符，长度在8-10之间)");
                    alert.showAndWait();
                } else{
                    //加入数据库
                    try{
                        birthday = birth.getValue().toString();
                        address = provinces.getValue().toString();
                    }catch (Exception e){
                        alert.setContentText("请检查日期和住址");
                        alert.showAndWait();
                        return ;
                    }
                    if(jfxRadioButton1.isSelected()){
                        gender="男";
                    }else {
                        gender="女";
                    }
                    if(jfxRadioButton11.isSelected()){
                        identity="老师";
                    }else{
                        identity="学生";
                    }
                    //
                    RegisterJDBC registerJDBC = new RegisterJDBC();
                    registerJDBC.initial();
                    if(registerJDBC.register(user,pass,gender,address,birthday,identity)){
                        alert.setContentText("注册成功！！！");
                        alert.showAndWait();
                    }else {
                        alert.setContentText("注册失败 系统出现故障！！！");
                        alert.showAndWait();
                    }

                }
            }else {
                alert.setContentText("请确认输入账户名为字母开头加数字");
                alert.showAndWait();

            }

        }



    }





}
